package com.yuanfeng.commoms.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.yuanfeng.commoms.dto.oos.OssUploadUpdateParamDTO;
import com.yuanfeng.commoms.util.secverifyapiutil.Base64Utils;
import org.springframework.util.ResourceUtils;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * 二维码工具类
 *
 * @author limingcheng
 */
public class QrCodeUtil {

    private static final String GETWXACODEUNLIMIT_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s";// 生成小程序码地址
    private static final String URL_GET_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";//获取access_token地址
    private static final String APP_ID = "";// 小程序appid
    private static final String APP_SECRET = ""; // 小程序秘钥

    public static final String PATH_HOME = "";
    public static final String BASE_PREFIX = "data:image/png;base64,"; // base64图片固定前缀

    /**
     * 生成一个二维码图片
     *
     * @param width
     * @param height
     * @param content
     * @return
     * @throws WriterException
     * @throws IOException
     */

    public static byte[] createQRCode(int width, int height, String content){
        ByteArrayOutputStream os = null;

        try {
            // 二维码基本参数设置

            Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();

            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码字符集utf-8

            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 设置纠错等级L/M/Q/H,纠错等级越高越不易识别，当前设置等级为最高等级H

            hints.put(EncodeHintType.MARGIN, 0);// 可设置范围为0-10，但仅四个变化0 1(2) 3(4 5 6) 7(8 9 10)

            // 生成图片类型为QRCode

            BarcodeFormat format = BarcodeFormat.QR_CODE;

            // 创建位矩阵对象

            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, format, width, height, hints);

            // 设置位矩阵转图片的参数

            //  MatrixToImageConfig config = new MatrixToImageConfig(Color.black.getRGB(), Color.white.getRGB());

            // 位矩阵对象转流对象

            os = new ByteArrayOutputStream();

            MatrixToImageWriter.writeToStream(bitMatrix, "png", os);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return os.toByteArray();

    }

    public static String GenerateImage(String imgStr, String imgFilePath) {// 对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) // 图像数据为空
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] bytes = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            String dateString = formatter.format(currentTime);
            String pathUrl = ResourceUtils.getURL("classpath:").getPath();
            String path1 = pathUrl+"/static/images/"+ dateString+".jpg";
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(path1);
            out.write(bytes);
            out.flush();
            out.close();
            return dateString+".jpg";
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 生成二维码上传oss
     */
    public static ResponseResult  GenerateQRCode(String code){
            byte[] b = createQRCode(150, 150, code);
            //上传oss
            String encode = Base64Utils.encode2(b);
            OssUploadUpdateParamDTO param = new OssUploadUpdateParamDTO();
            param.setBase64Str(encode);
            param.setStreamName(".png");
            param.setFileUrl("file/upload/shop/settled/");
            return ResponseResult.success(param);
    }
}
